package com.itcodai.dbconnect.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author c-fanqj
 * @date 2023/3/29
 * @apiNote
 */
//@EnableTransactionManagement
//@Configuration
public class MyBatisConfig {

    //@Resource(name = "myRoutingDataSource")
    private DataSource myRoutingDataSource;

    //@Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver()
                        //.getResources("classpath:mapper/*/*.xml"));
                        .getResources("classpath:mapper/**/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itcodai.entity");
        return sqlSessionFactoryBean.getObject();
    }

    //@Bean
    public PlatformTransactionManager platformTransactionManager() {
        return new DataSourceTransactionManager(myRoutingDataSource);
    }

}
